<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/8
 * Time: 下午1:35
 */
class ServerService extends Service{
	
	public static function opsIndexHeader(){
		$header = array(
				'id'            => array('text' => '序号', 'width' => '90px'),
				'server_name'   => array('text' => '服务器名称', 'width' => '160px'),
				'server_num'    => array('text' => '服务器ID', 'width' => '100px'),
				'sort_num'      => array('text' => '排序', 'width' => '50px'),
				'name'          => array('text' => '游戏结点名', 'width' => '100px'),
				'show_status'   => array('text' => '表现状态', 'width' => '100px'),
				'is_wlk'        => array('text' => '开服状态', 'width' => '100px'),
				'develop_id'    => array('text' => '开发ID', 'width' => '160px'),
				'special_name'   => array('text' => '所属专服', 'width' => '160px'),
				'oss_api'       => array('text' => 'OSS API', 'width' => '200px'),
				'log_server'    => array('text' => '日志服务器', 'width' => '100px'),
				'ip_port'       => array('text' => 'IP:端口', 'width' => '120px'),
				'open_time'     => array('text' => '开服时间', 'width' => '140px'),
		);
	
		return $header;
	}
	
	public static function opsIndexData($params){
		$user = AccAuth::me();
		$specialList = array();
	
		$modServer = new ModServer();
		$result = $modServer->page($params);
	
		$modSpecialServicer = new ModSpecialServicer();
		$spList = $modSpecialServicer->getSpecialServer();
		$specialList = array();
		foreach ($spList as $key=>$val){
			$specialList[$val['server_id']]['name'] = $val['name'];
			$specialList[$val['server_id']]['cname'] = $val['cname'];
			$specialList[$val['server_id']]['special_flag_id'] = $val['special_flag_id'];
		}
	
		$length = count($result['data']);
		$showStatusList = self::showStatusList();
	    $is_wlkList = self::is_wlkList();
		foreach($result['data'] as &$v){
			$type = 1;
			if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
				$type = 2;
			} 
			$v['special_name'] = $specialList[$v['id']]['name'];
			
			$id = $v['id'];
			$checkbox = '<input value="'.$v['id'].'" data-status="'.$v['show_status'].'" data-tmp="'.$v['open_time'].'" data-wlk="'.$v['is_wlk'].'" name="id" type="checkbox">&nbsp;&nbsp;';
			$v['id'] = $checkbox.$v['id'];
			
			$v['show_status'] = $showStatusList[$v['show_status']];
			$v['open_time']     = $v['open_time']? date('Y-m-d H:i:s', $v['open_time']) : '';
			$f = $v['port']? ':' : '';
			$v['ip_port'] = $v['ip'].$f.$v['port'];
			$v['is_wlk'] = $is_wlkList[$v['is_wlk']];
			
	
		}
		unset($v);
	
		return $result;
	}

    public static function indexHeader(){
        $header = array(
            'id'            => array('text' => '序号', 'width' => '90px'),
            'server_name'   => array('text' => '服务器名称', 'width' => '160px'),
        	'server_num'    => array('text' => '服务器ID', 'width' => '100px'),
        	'sort_num'      => array('text' => '排序', 'width' => '50px'),
        	'name'          => array('text' => '游戏结点名', 'width' => '160px'),
        	'show_status'   => array('text' => '表现状态', 'width' => '160px'),
        	'develop_id'    => array('text' => '开发ID', 'width' => '160px'),
        	'special_name'   => array('text' => '所属专服', 'width' => '160px'),
        	'special_cname'   => array('text' => '专服别名', 'width' => '160px'),
        	'special_flag_id'   => array('text' => '专服ID', 'width' => '100px'),
        	'channel_list'   => array('text' => '渠道列表', 'width' => '200px'),
            'oss_api'       => array('text' => 'OSS API', 'width' => '200px'),
            'log_server'    => array('text' => '日志服务器', 'width' => '100px'),
        	'check_flag'    => array('text' => '服标记', 'width' => '80px'),
        	'ip_port'       => array('text' => 'IP:端口', 'width' => '120px'),
        	'open_time'     => array('text' => '开服时间', 'width' => '140px'),
        	'union_time'    => array('text' => '合服时间', 'width' => '140px'),
        	'hefu_flag'    => array('text' => '合服标记', 'width' => '80px'),
            //'status_text'   => array('text' => '状态', 'width' => '60px'),
        	'is_syn'        => array('text' => '同步状态', 'width' => '60px'),
            'action'        => array('text' => '操作', 'width' => '60px'),
        );

        return $header;
    }
    
    
    public static function qindexHeader(){
    	$header = array(
    			'id'            => array('text' => '序号', 'width' => '90px'),
    			'server_name'   => array('text' => '服务器名称', 'width' => '160px'),
    			'server_num'    => array('text' => '服务器ID', 'width' => '100px'),
    			'sort_num'      => array('text' => '排序', 'width' => '50px'),
    			'show_status'   => array('text' => '表现状态', 'width' => '160px'),
    			'special_name'   => array('text' => '所属专服', 'width' => '160px'),
    			'special_cname'   => array('text' => '专服别名', 'width' => '160px'),
    			'special_flag_id'   => array('text' => '专服ID', 'width' => '100px'),
    			'channel_list'   => array('text' => '渠道列表', 'width' => '200px'),
    			'check_flag'    => array('text' => '服标记', 'width' => '80px'),
    			'open_time'     => array('text' => '开服时间', 'width' => '140px'),
//     			'union_time'    => array('text' => '合服时间', 'width' => '140px'),
//     			'hefu_flag'    => array('text' => '合服标记', 'width' => '80px'),
    			'is_syn'        => array('text' => '同步状态', 'width' => '60px'),
    			'action'        => array('text' => '操作', 'width' => '60px'),
    	);
    
    	return $header;
    }

    public static function indexData($params){
    	$user = AccAuth::me();
    	$specialList = array();
//     	if($user['IsAdmin'] != 1){
//     		$options = PubAuthSession::get('os');
//     		if($options){
//     			$tabs = '';
//     			foreach ($options as $key=>$val){
//     				$tabs .= ','.'"'.$key.'"';
//     			}
//     			$tabs = substr($tabs, 1);
    	
//     			$modChannel = new ModChannel();
//     			$special = $modChannel->getSpecialForChannel($tabs);
//     			$specialList = array();
//     			foreach ($special as $key=>$val){
//     				$specialList[] = $val['special_id'];
//     			}
//     		}
//     		if(!in_array($params['special_id'], $specialList)){  //已经包含了special_id 为空的时候
//     			$params['special_id'] = -1;
    			
//     		}
//     	}

        $modServer = new ModServer();
        $result = $modServer->page($params);
        
        $modSpecialServicer = new ModSpecialServicer();
        $spList = $modSpecialServicer->getSpecialServer();
        $specialList = array();
        foreach ($spList as $key=>$val){
        	$specialList[$val['server_id']]['name'] = $val['name'];
        	$specialList[$val['server_id']]['cname'] = $val['cname'];
        	$specialList[$val['server_id']]['special_flag_id'] = $val['special_flag_id'];
        }

        $statusMap = self::statusList();
        $length = count($result['data']);
        $checkFlagList = self::checkFlagList();
        $hefuFlagList = self::hefuFlagList();
        $isSynList = self::isSynList();
        $showStatusList = self::showStatusList();
        foreach($result['data'] as $k=>&$v){
        	$type = 1;
        	if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
        		$type = 2;
        	}
        	
        	$v['channel_list'] = self::getChannel($v['id']);
        	
        	$v['special_name'] = $specialList[$v['id']]['name'];
        	$v['special_cname'] = $specialList[$v['id']]['cname'];
        	$v['special_flag_id'] = $specialList[$v['id']]['special_flag_id'];
            $v['open_time']     = $v['open_time']? date('Y-m-d H:i:s', $v['open_time']) : '';
            $v['union_time']     = $v['union_time']? date('Y-m-d H:i:s', $v['union_time']) : '';
            $f = $v['port']? ':' : '';
            $v['ip_port'] = $v['ip'].$f.$v['port'];
            $v['hefu_flag'] = $hefuFlagList[$v['hefu_flag']];
            $v['check_flag'] = $checkFlagList[$v['check_flag']];
            //$v['status_text']   = $statusMap[$v['status']];
            $v['is_syn'] = $isSynList[$v['is_syn']];
            $show_status = $v['show_status'];
            $v['show_status'] = $showStatusList[$v['show_status']];
            
            //$v['action']        = '<a href="'.PubFunc::buildUrl('/oss/?r=server/setChannel').'&id='.$v['id'].'">开通渠道</a>';
            if($params['is_qu'] == 0){
            	$v['action']        = ' <a href="'.PubFunc::buildBackUrl('/oss/?r=server/edit').'&id='.$v['id'].'">编辑</a>';
            }
            if($params['is_qu'] == 1){
            	$v['action']        = ' <a href="'.PubFunc::buildBackUrl('/oss/?r=server/qedit').'&id='.$v['id'].'">编辑</a>';
            }
            
            if($user['IsAdmin'] == 1){
            	$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=server/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
            }
            
            $id = $v['id'];
            $checkbox = '<input value="'.$v['id'].'" data-status="'.$show_status.'" name="id" type="checkbox">&nbsp;&nbsp;';
            $v['id'] = $checkbox.$v['id'];
  
        }
        unset($v);

        return $result;
    }
    
    public static function getChannel($id){
    	$modServer = new ModServer();
    	$channelData = $modServer->getChannel($id);
    	$list = '';
    	foreach ($channelData as $key=>$val){
    		$list .= ','.$val['channel_name'];
    	}
    	$list = substr($list, 1);
    	return $list;
    }

    public static function doAdd($params,$api_synchro_url){
    	$return_result = array();
        $user = AccAuth::me();

        $modServer = new ModServer();

        $modServer->platform      = 'test';
        $modServer->game          = $params['g'];
        $modServer->server_num    = $params['server_num'];
        $modServer->server_name   = $params['server_name'];
        $modServer->develop_id    = $params['develop_id'];
        $modServer->oss_api       = $params['oss_api'];
        $modServer->log_server    = $params['log_server'];
//        $modServer->cdn_url       = $params['cdn_url'];
//        $modServer->client_url    = $params['client_url'];
       $modServer->ip            = $params['ip'];
       $modServer->port          = $params['port'];
       $modServer->open_time     = strtotime($params['open_time']);
       $modServer->union_time    = strtotime($params['union_time']);
       $modServer->union_to      = $params['union_server'];
        $modServer->status        = 1; // 废弃字段
        $modServer->create_user   = $user['UserID'];
        $modServer->create_time   = time();
        $modServer->update_user   = $user['UserID'];
        $modServer->update_time   = time();
        $modServer->hefu_flag   = $params['hefu_flag'];
        $modServer->check_flag   = $params['check_flag'];
        $modServer->show_status = $params['show_status'];
        $modServer->float_status = 1;  //该值已经弃用
        $modServer->name = $params['name'];
        $modServer->sort_num = $params['sort_num'];
        $result = $modServer->save();
        if($result !== false){
        	//记录服务器与渠道的关系
        	$channel_array = $params['channel_id'];
        	$modServerChannel = new ModServerChannel();
        	$channel = array();
        	foreach ($channel_array as $key=>$val){
        		$data = array();
        		$data['channel_id'] = $val;
        		$data['server_id'] = $modServer->id;
        		array_push($channel, $data);
        	}
        	$modServerChannel->addServerChannel($channel);
        	//记录服务器与渠道的关系
        	
        	//记录服务器与专服的关系
        	$modSpecialServicer = new ModSpecialServicer();
        	$modSpecialServicer->special_id = $params['special_id'];
        	$modSpecialServicer->server_id = $modServer->id;
        	$modSpecialServicer->save();
        	//记录服务器与专服的关系
        	
        	//同步游戏服务器的配置
        	$synchro = array();
        	$synchro['name'] = $params['name'];
        	$synchro['show_status'] = intval($params['show_status']);
        	$synchro['ip'] = $params['ip'];
        	$synchro['port'] = intval($params['port']);
        	$synchro['float_status'] = 1;
        	$synchro['open_time'] = strtotime($params['open_time']);
        	$synchro['show_name'] = $params['server_name'];
        	$synchro['server_id'] = $params['server_num'];
        	$api = new ApiSynchro();
        	$result = $api->gameAdd($synchro,$api_synchro_url);
        	//$result['state'] = true;
        	if($result['state']){ //同步成功
        		//同步服务器与渠道的配置关系
        		self::channel_syn($channel_array,$params['g'],$modServer->id,2,$api_synchro_url);
        		
        	}else{
        	   //停止同步操作
        		$update = array();
        		$update['is_syn'] = 2;
        		$update['syn_time'] = time();
        		$modServer->updateByPk($modServer->id, $update);
        		//请求失败
        		$return_result = array('state'=>false,'msg'=>'同步'.$isresult['msg']);
        		return $return_result;
        	}
        }
        $return_result = array('state'=>true,'msg'=>'成功');
        return $return_result;
    }
    
    public static function channel_syn($channel_array,$game,$c_id,$is_syn,$api_synchro_url){    	
    	//同步服务器与渠道的配置关系
    	$api = new ApiSynchro();
    	$modServer = new ModServer();
    	$modServerChannel = new ModServerChannel();
    	foreach ($channel_array as $key=>$val){
    		$channel_id = $val;
    		$gameList = $modServerChannel->selectServerForChannel1($channel_id,$game,$api_synchro_url);
    		$channelConfig = array();
    		
    		if(count($gameList) == 0){
    			//查询tab
    			$modChannel = new ModChannel();
    			$obj = $modChannel->findByPk($channel_id);
    			$info = $obj->getAttributes();
    			$channelConfig['channel_id'] = $info['tab'];
    			$channelConfig['game_list'] = array();
    				
    		}else{
    			foreach ($gameList as $gkey=>$gval){
    				$channelConfig['channel_id'] = $gval['tab'];
    				$channelConfig['game_list'][] = $gval['name'];
    			}
    		}
    		
//     		echo '<pre/>';
//     		echo $api_synchro_url;echo '<br/>';
//     		var_dump($channelConfig);
    		 
    		
    		//先查询channel_id是否已经存在，存在则修改，不存在则添加
    		$isresult = $api->channelGet($channelConfig['channel_id'],$api_synchro_url);
    		//$isresult['state'] = true;
    		if($isresult['state']){
    			if(count($isresult['data']) == 0){ //添加
    				$aeresult = $api->channelAdd($channelConfig,$api_synchro_url);
    			}else{ //编辑
    				$aeresult = $api->channelEdit($channelConfig,$api_synchro_url);
    			}
    	
    			$update = array();
    			$update['syn_time'] = time();
    			if(!$aeresult['state']){    				
    				$update['is_syn'] = $is_syn;
    			}else{
    				$update['is_syn'] = 1;
    			}
    			$modServer->updateByPk($c_id, $update);
    		}else{
    			//停止同步
    			$update = array();
    			$update['is_syn'] = $is_syn;
    			$update['syn_time'] = time();
    			$modServer->updateByPk($c_id, $update);
    		}
    	}
    }

    public static function doEdit($id, $params,$api_synchro_url){
    	$return_result = array();
        
        if(empty($id)){
        	$return_result = array('state'=>false,'msg'=>'非法参数');
        	return $return_result;
        }

        $user = AccAuth::me();

        $modServer = new ModServer();
        $update = array();
        $update['platform']     = 'test';
        $update['game']         = $params['g'];
        $update['server_num']   = $params['server_num'];
        $update['server_name']  = $params['server_name'];
        $update['develop_id']   = $params['develop_id'];
        $update['oss_api']      = $params['oss_api'];
        $update['log_server']  = $params['log_server'];
       $update['ip']           = $params['ip'];
       $update['port']         = $params['port'];
       $update['open_time']    = strtotime($params['open_time']);
       $update['union_time']   = strtotime($params['union_time']);
       $update['union_to']     = $params['union_server'];
        //$update['status']       = $params['status']; // 废弃字段
        $update['update_user']  = $user['UserID'];
        $update['update_time']  = time();
        $update['hefu_flag']   = $params['hefu_flag'];
        $update['check_flag']   = $params['check_flag'];
        $update['show_status']   = $params['show_status'];
        //$update['float_status']   = $params['float_status'];
        $update['name']   = $params['name'];
        $update['sort_num']   = $params['sort_num'];
        $result = $modServer->updateByPk($id, $update);
        
        if($result !== false){
        	//修改服务器对应的专服
        	//先删除
        	$modSpecialServicer = new ModSpecialServicer();
        	$modSpecialServicer->deleteAll('server_id=:server_id', array(':server_id'=>$id));
        	//再添加
        	$modSpecialServicer->special_id = $params['special_id'];
        	$modSpecialServicer->server_id = $id;
        	$modSpecialServicer->save();
        	//$modSpecialServicer->updateAll(array('special_id'=>$params['special_id'],'server_id'=>$id),'server_id=:server_id',array(':server_id'=>$id));
        	
        	//修改服务器对应的渠道
        	//先删除
        	$modServerChannel = new ModServerChannel();
        	$modServerChannel->deleteAll('server_id=:server_id', array(':server_id'=>$id));
        	//再添加
        	$channel_array = $params['channel_id'];
        	$channel = array();
        	foreach ($channel_array as $key=>$val){
        		$data = array();
        		$data['channel_id'] = $val;
        		$data['server_id'] = $id;
        		array_push($channel, $data);
        	}
        	$modServerChannel->addServerChannel($channel);
        	
        	//同步游戏服务器的配置
        	$synchro = array();
        	$synchro['name'] = $params['name'];
        	$synchro['show_status'] = intval($params['show_status']);
        	$synchro['ip'] = $params['ip'];
        	$synchro['port'] = intval($params['port']);
        	$synchro['float_status'] = 1;
        	$synchro['open_time'] = strtotime($params['open_time']);
        	$synchro['show_name'] = $params['server_name'];
        	$synchro['server_id'] = $params['server_num'];
        	$api = new ApiSynchro();
        	$isresult = $api->gameGet($params['name'],$api_synchro_url);        	
        	//$isresult['state'] = true;
        	if($isresult['state']){
    			if(count($isresult['data']) == 0){ //添加
    				$result = $api->gameAdd($synchro,$api_synchro_url);
    			}else{ //编辑
    				$result = $api->gameEdit($synchro,$api_synchro_url);
    			}
        	}else{
        		$update = array();
        		$update['is_syn'] = 3;
        		$update['syn_time'] = time();
        		$modServer->updateByPk($id, $update);
        		//请求失败
        		$return_result = array('state'=>false,'msg'=>'同步'.$isresult['msg']);
        		return $return_result;
        	}
        	
        	if($result['state']){ //同步成功
        		//同步服务器与渠道的配置关系
        		$old_channel = $params['old_channel'];
        		$old_channel_array = explode(",",$old_channel);
        		$specialinfo = SpecialServerService::get($params['old_special_id']);
        		if($specialinfo['api_synchro_url'] == $api_synchro_url){  //同步地址相同，则一起同步
        			$channel_array = array_merge($old_channel_array,$channel_array);
        			$channel_array = array_unique($channel_array);
        			self::channel_syn($channel_array,$params['g'],$id,3,$api_synchro_url);
        		}else{ //同步地址不同，新旧分开同步
        			self::channel_syn($old_channel_array,$params['g'],$id,3,$specialinfo['api_synchro_url']);  //旧的更新
        			self::channel_syn($channel_array,$params['g'],$id,3,$api_synchro_url);  //新的更新
        		}
	
        	}else{
        	   //停止同步操作
        		$update = array();
        		$update['is_syn'] = 3;
        		$update['syn_time'] = time();
        		$modServer->updateByPk($id, $update);
        	}
        	
        }
        $return_result = array('state'=>true,'msg'=>'成功');
        return $return_result;
    }
    
    
    public static function qdoEdit($id, $params,$api_synchro_url){
    	$return_result = array();
    
    	if(empty($id)){
    		$return_result = array('state'=>false,'msg'=>'非法参数');
    		return $return_result;
    	}
    
    	$user = AccAuth::me();
    
    	$modServer = new ModServer();
    	$update = array();
    	$update['platform']     = 'test';
    	$update['game']         = $params['g'];
    	$update['server_num']   = $params['server_num'];
    	$update['server_name']  = $params['server_name'];
    	$update['open_time']    = strtotime($params['open_time']);
    	$update['update_user']  = $user['UserID'];
    	$update['update_time']  = time();
    	$update['check_flag']   = $params['check_flag'];
    	$update['show_status']   = $params['show_status'];
    	$update['sort_num']   = $params['sort_num'];
    	$result = $modServer->updateByPk($id, $update);
    
    	if($result !== false){
    		//修改服务器对应的专服
    		//先删除
    		$modSpecialServicer = new ModSpecialServicer();
    		$modSpecialServicer->deleteAll('server_id=:server_id', array(':server_id'=>$id));
    		//再添加
    		$modSpecialServicer->special_id = $params['special_id'];
    		$modSpecialServicer->server_id = $id;
    		$modSpecialServicer->save();
    		//$modSpecialServicer->updateAll(array('special_id'=>$params['special_id'],'server_id'=>$id),'server_id=:server_id',array(':server_id'=>$id));
    		 
    		//修改服务器对应的渠道
    		//先删除
    		$modServerChannel = new ModServerChannel();
    		$modServerChannel->deleteAll('server_id=:server_id', array(':server_id'=>$id));
    		//再添加
    		$channel_array = $params['channel_id'];
    		$channel = array();
    		foreach ($channel_array as $key=>$val){
    			$data = array();
    			$data['channel_id'] = $val;
    			$data['server_id'] = $id;
    			array_push($channel, $data);
    		}
    		$modServerChannel->addServerChannel($channel);
    		 
    		//查询当前服务器的信息
    		$qinfo = self::get($id);
    		//同步游戏服务器的配置
    		$synchro = array();
    		$synchro['name'] = $qinfo['name'];
    		$synchro['show_status'] = intval($qinfo['show_status']);
    		$synchro['ip'] = $qinfo['ip'];
    		$synchro['port'] = intval($qinfo['port']);
    		$synchro['float_status'] = 1;
    		$synchro['open_time'] = strtotime($params['open_time']);
    		$synchro['show_name'] = $params['server_name'];
    		$synchro['server_id'] = $params['server_num'];
    		$api = new ApiSynchro();
    		$isresult = $api->gameGet($qinfo['name'],$api_synchro_url);
    		//$isresult['state'] = true;
    		if($isresult['state']){
    			if(count($isresult['data']) == 0){ //添加
    				$result = $api->gameAdd($synchro,$api_synchro_url);
    			}else{ //编辑
    				$result = $api->gameEdit($synchro,$api_synchro_url);
    			}
    		}else{
    			$update = array();
    			$update['is_syn'] = 3;
    			$update['syn_time'] = time();
    			$modServer->updateByPk($id, $update);
    			//请求失败
    			$return_result = array('state'=>false,'msg'=>'同步'.$isresult['msg']);
    			return $return_result;
    		}
    		 
    		if($result['state']){ //同步成功
    			//同步服务器与渠道的配置关系
    			$old_channel = $params['old_channel'];
    			$old_channel_array = explode(",",$old_channel);
    			$specialinfo = SpecialServerService::get($params['old_special_id']);
    			if($specialinfo['api_synchro_url'] == $api_synchro_url){  //同步地址相同，则一起同步
    				$channel_array = array_merge($old_channel_array,$channel_array);
    				$channel_array = array_unique($channel_array);
    				self::channel_syn($channel_array,$params['g'],$id,3,$api_synchro_url);
    			}else{ //同步地址不同，新旧分开同步
    				self::channel_syn($old_channel_array,$params['g'],$id,3,$specialinfo['api_synchro_url']);  //旧的更新
    				self::channel_syn($channel_array,$params['g'],$id,3,$api_synchro_url);  //新的更新
    			}
    
    		}else{
    			//停止同步操作
    			$update = array();
    			$update['is_syn'] = 3;
    			$update['syn_time'] = time();
    			$modServer->updateByPk($id, $update);
    		}
    		 
    	}
    	$return_result = array('state'=>true,'msg'=>'成功');
    	return $return_result;
    }
    
    /**
     * 开通渠道
     * @param unknown $params
     * @return boolean|unknown
     */
    public static function doChannel($params){
    	if(empty($params['server_id'])) return false;
    	
    	$modServerChannel = new ModServerChannel();
    	$channel = array();
    	foreach ($params['channel_id'] as $key=>$val){
    		$data = array();
    		$data['server_id'] = $params['server_id'];
    		$data['channel_id'] = $val;
    		array_push($channel, $data);
    	}
    	$result = $modServerChannel->addServerChannel($channel);
    	return $result;

    }
    
    /**
     * 获取已经设置的渠道
     * @param unknown $id
     * @return multitype:unknown
     */
    public static function alreadyChannel($id){
    	$modServerChannel = new ModServerChannel();
    	$params['server_id'] = $id;
    	$list = $modServerChannel->select($params);
    	$already = array();
    	foreach ($list as $key=>$val){
    		$already[] = $val['channel_id'];
    	}
    	return $already;
    }
    

    public static function get($id){
        $modServer = new ModServer();
        $obj = $modServer->findByPk($id);

        if($obj){
            $info = $obj->getAttributes();

            $info['open_time']  = empty($info['open_time']) ? '' : date('Y-m-d H:i:s', $info['open_time']);
            $info['union_time'] = empty($info['union_time']) ? '' : date('Y-m-d H:i:s', $info['union_time']);

            return $info;
        }else{
            return false;
        }
    }

    /**
     * 删除服务器，也需删除对应的专服的关系，也需删除对应的渠道关系
     * @param unknown $id
     * @return boolean
     */
    public static function delete($id){
        if(empty($id)) return false;

        $modServer = new ModServer();
        $modServer->deleteByPk($id);
        
        //删除服务器对应的专服
        $modSpecialServicer = new ModSpecialServicer();
        $modSpecialServicer->deleteAll('server_id=:server_id', array(':server_id'=>$id));
        
        //删除服务器对应的渠道
        $modServerChannel = new ModServerChannel();
        $modServerChannel->deleteAll('server_id=:server_id', array(':server_id'=>$id));
        
        return true;
    }
    
    public static function batchDelete($server_id){
    	$modServerChannel = new ModServerChannel();
    	$modServerChannel->deleteAll('server_id=:server_id', array(':server_id'=>$server_id));
    	return true;
    }

    public static function statusList(){
        return array(
            '1'     => '正常',
        	'2'     => '测试',
            //'2'     => '外部测试',
            //'3'     => '内部测试',
        	'4'    => '合服',
            '9'     => '维护',
            '-1'    => '关闭',
        	
        );
    }

    public static function serverTypeList(){
        return array(
            '1' => '正式',
            '2' => '沙箱'
        );
    }
    
    public static function showStatusList(){
    	return array(
    			'1' => '正常',
    			'2' => '特殊状态',
    			'3' => '维护',
    	);
    }
    
    public static function is_wlkList(){
    	return array(
    			'1' => '已开服',
    			'2' => '已停服',
    	);
    }
    

    public static function getOssApi($id){
        $modServer = new ModServer();
        $obj = $modServer->findByPk($id);
        return isset($obj->oss_api) ? $obj->oss_api : '';
    }

    public static function getOptions($platform=null, $game=null){

        if(empty($platform)){
            $platform = PubBase::getCurrentPlatform();
        }
        if(empty($game)){
            $game = PubBase::getCurrentGame();
        }

        $modServer = new ModServer();
        $result = $modServer->select(array(
            'p' => $platform,
            'g' => $game
        ));
        $options = array();
        foreach($result as $v){
            $options[$v['id']] = $v['server_name'].' ('.$v['server_num'].')';
        }
        return $options;
    }
    
    public static function getOptions1(){
        $os_options = PubAuthSession::get('os');
    	$os_array = array();
        foreach ($os_options as $key=>$val){
        	$os_array[] = $key;
        }
        
        $options = array();
        if($os_array){
        	$os = $os_array[0];
        	$game = PubBase::getCurrentGame();
        	$modChannel = new ModChannel();
        	$server_list = $modChannel->getServer($os,$game);
        	foreach ($server_list as $key=>$val){
        		$options[$val['id']] = $val['server_name'].' ('.$val['server_num'].')';
        	}
        }
        
        return $options;
    }

    public static function sign($platform, $game, $serverNum, $serverName, $serverType, $openTime, $unionTo, $status){

    }
    
    /*
     * 去重
     */
    public static function repeat($id = 0, $field, $val,$g){
    	$modServer = new ModServer();
    	$conut = $modServer->repeat($id, $field, $val,$g);
    	return $conut;
    }
    
    public static function getSpecialServer($status){
    	$modServer = new ModServer();
    	$list = $modServer->getSpecialServer($status);
    	$data = array();
    	foreach ($list as $kry=>$val){
    		$data[$val['name']][] = $val;
    	}
    	return $data;
//     	echo '<pre>';
//     	var_dump($data);exit;
    }
    
    public static function getServerForSpecialId($g,$special_id){
    	$modServer = new ModServer();
    	$list = $modServer->getServerForSpecialId($g,$special_id);
    	return $list;
    }
    
    public static function checkFlagList(){
    	return array(
    			'1' => '测试服',
    			'2' => '正式服',
    			'3' => '审核服'
    	);
    }
    
    public static function hefuFlagList(){
    	return array(
    			'1' => '未合服',
    			'2' => '已合服',
    			'3' => '主服',
    	);
    }
    
    public static function isSynList(){
    	return array(
    			'1' => '成功',
    			'2' => '失败(添加时)',
    			'3' => '失败(编辑时)',
    	);
    }
    
    /**
     * 开服
     * @param unknown $ids
     * @return multitype:number string
     */
    public static function open_server($ids){   	
    	//根据ids查询出服务器列表信息
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    	$tmp = time();
    	$show_flag = true;
    	$info = '表现状态须为 特殊状态或维护状态 且开服状态为 已停服 且 开服时间小于当前时间的服务器才能进行开服，序号为  ';
    	foreach ($list as $key=>$val){
    		if($val['show_status'] != 2 && $val['show_status'] != 3 || $val['open_time'] > $tmp || $val['is_wlk'] != 2){
    			$show_flag = false;
    			$info .= $val['id'] . ', ';
    		}
    	}
    	
    	$info = substr($info,0,strlen($info)-2); 
    	$info .= ' 的数据不符合开服的条件';
    	if(!$show_flag){
    		$out = array('status'=>0,'msg'=>$info,'data'=>'');
    		return $out;
    	}
    	
    	$rInfo = '';
    	foreach ($list as $key=>$val){
    		$exec_l = 'sudo /data/sh/web/wstart_or_stop_game.sh start '.$val['develop_id'].' '.$val['ip']; 
    		//$exec_l = 'dir';
    		exec($exec_l,$output,$return_var);
    	    $jb_out = '';
    		foreach ($output as $ok=>$ov){
    			$jb_out .= $ov.",\n<br/>";
    		}
    		
    		if($return_var == 0){  //成功,则需改变状态且改变游戏边的状态,改为 特殊状态
    			$opsInfo = self::opsUpdate($val,2,1,0);
    			if($opsInfo == ''){ //修改状态和同步成功
    				$rInfo .= "序号：".$val['id']." 的数据开服成功; "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}else{
    				$rInfo .= "序号：".$val['id']." 的数据执行开服脚本成功， ".$opsInfo."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}
    		}else{
    			$rInfo .= "序号：".$val['id']." 的数据执行开服脚本失败; "."\n<br/>";
    			$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    		}
    	}
    	
    	$out = array('status'=>1,'msg'=>$rInfo,'data'=>'');
    	return $out;
    }
    
    /**
     * 停服
     * @param unknown $ids
     * @return multitype:number string
     */
    public static function stop_server($ids){
    	//根据ids查询出服务器列表信息
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    	$tmp = time();
    	$show_flag = true;
    	$info = '表现状态须为 正常状态或特殊状态 且 开服状态为 已开服 的服务器才能进行停服，序号为  ';
    	foreach ($list as $key=>$val){
    		if($val['show_status'] != 1 && $val['show_status'] != 2 || $val['is_wlk'] != 1){
    			$show_flag = false;
    			$info .= $val['id'] . ', ';
    		}
    	}
    	 
    	$info = substr($info,0,strlen($info)-2);
    	$info .= ' 的数据不符合停服的条件';
    	if(!$show_flag){
    		$out = array('status'=>0,'msg'=>$info,'data'=>'');
    		return $out;
    	}
    	 
    	$rInfo = '';
    	foreach ($list as $key=>$val){
    		$exec_l = 'sudo /data/sh/web/wstart_or_stop_game.sh stop '.$val['develop_id'].' '.$val['ip'];
    		//$exec_l = 'dir';
    		exec($exec_l,$output,$return_var);
    		$jb_out = '';
    		foreach ($output as $ok=>$ov){
    			$jb_out .= $ov.",\n<br/>";
    		}
    		
    		if($return_var == 0){  //成功,则需改变状态且改变游戏边的状态
    			$opsInfo = self::opsUpdate($val,2,2,0);
    			if($opsInfo == ''){ //修改状态和同步成功
    				$rInfo .= "序号：".$val['id']." 的数据停服成功; "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}else{
    				$rInfo .= "序号：".$val['id']." 的数据执行停服脚本成功， ".$opsInfo."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}
    		}else{
    			$rInfo .= "序号：".$val['id']." 的数据执行停服脚本失败; "."\n<br/>";
    			$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    		}
    	}
    	
    	$out = array('status'=>1,'msg'=>$rInfo,'data'=>'');
    	return $out;
    }
    
    
    /**
     * 清档
     * @param unknown $ids
     * @return multitype:number string
     */
    public static function clear_gear($ids){
    	//根据ids查询出服务器列表信息
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    	$tmp = time();
    	$show_flag = true;
    	$info = '表现状态须为 特殊状态或维护状态 且开服状态为 已停服 且 开服时间大于当前时间 的服务器才能进行清档，序号为 ';
    	foreach ($list as $key=>$val){
    		if($val['show_status'] != 2 && $val['show_status'] != 3 || $val['open_time'] < $tmp || $val['is_wlk'] != 2){
    			$show_flag = false;
    			$info .= $val['id'] . ', ';
    		}
    	}
    	 
    	$info = substr($info,0,strlen($info)-2);
    	$info .= ' 的数据不符合清档的条件';
    	if(!$show_flag){
    		$out = array('status'=>0,'msg'=>$info,'data'=>'');
    		return $out;
    	}
    	 
    	$rInfo = '';
    	foreach ($list as $key=>$val){
    		$exec_l = 'sudo /data/sh/web/wupdate_server_opentime.sh '.$val['develop_id'].' '.$val['ip'];
    		//$exec_l = 'dir';
    		exec($exec_l,$output,$return_var);
    		$jb_out = '';
    		foreach ($output as $ok=>$ov){
    			$jb_out .= $ov.",\n<br/>";
    		}
    		
    		if($return_var == 0){  //成功,则需改变状态且改变游戏边的状态,修改为特殊状态
    			$opsInfo = self::opsUpdate($val,2,1,0);
    			if($opsInfo == ''){ //修改状态和同步成功
    				$rInfo .= "序号：".$val['id']." 的数据清档成功; "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}else{
    				$rInfo .= "序号：".$val['id']." 的数据执行清档脚本成功， ".$opsInfo."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}
    		}else{
    			$rInfo .= "序号：".$val['id']." 的数据执行清档脚本失败; "."\n<br/>";
    			$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    		}
    	}
    	 
    	$out = array('status'=>1,'msg'=>$rInfo,'data'=>'');
    	return $out;
    }
    
    /**
     * 表现状态切换为正常状态
     * @param unknown $ids
     * @return multitype:number string
     */
    public static function update_show_status($ids){
    	//根据ids查询出服务器列表信息
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    	$tmp = time();
    	$show_flag = true;
    	$info = '表现状态须为 特殊状态 且开服状态为 已开服  的服务器才能把表现状态切换为正常状态，序号为 ';
    	foreach ($list as $key=>$val){
    		if($val['show_status'] != 2 || $val['is_wlk'] != 1){
    			$show_flag = false;
    			$info .= $val['id'] . ', ';
    		}
    	}
    	
    	$info = substr($info,0,strlen($info)-2);
    	$info .= ' 的数据不符合切换的条件';
    	if(!$show_flag){
    		$out = array('status'=>0,'msg'=>$info,'data'=>'');
    		return $out;
    	}
    	
    	$rInfo = '';
    	foreach ($list as $key=>$val){
    		$opsInfo = self::opsUpdate($val,1,1,0);
    		if($opsInfo == ''){ //修改为正常状态和同步成功
    			$rInfo .= "序号：".$val['id']." 的数据切换状态成功; "."\n<br/>";
    		}else{
    			$rInfo .= "序号：".$val['id']." ".$opsInfo."\n<br/>";
    		}
    	}
    	
    	$out = array('status'=>1,'msg'=>$rInfo,'data'=>'');
    	return $out;
    }
    
    
    public static function opsUpdate($val,$show_status,$is_wlk,$version_update = 0){
    	$modServer = new ModServer();
    	$update = array();
    	$update['show_status']   = $show_status;
    	$update['is_wlk']   = $is_wlk;
    	$update['version_update']   = $version_update;
    	$sq_result = $modServer->updateByPk($val['id'], $update);
    	if($sq_result !== false){
    		$synchro = array();
    		$synchro['name'] = $val['name'];
    		$synchro['show_status'] = intval($show_status);
    		$synchro['ip'] = $val['ip'];
    		$synchro['port'] = intval($val['port']);
    		$synchro['float_status'] = intval($val['float_status']);
    		$synchro['open_time'] = $val['open_time'];
    		$synchro['show_name'] = $val['server_name'];
    		$synchro['server_id'] = $val['server_num'];
    		$api = new ApiSynchro();
    		$s_result = $api->gameEdit($synchro,$val['api_synchro_url']);
    		//$s_result['state'] = true;
    		if(!$s_result['state']){ //同步失败
    			$update = array();
    			$update['is_syn'] = 3;
    			$update['syn_time'] = time();
    			$modServer->updateByPk($val['id'], $update);
    			$info= '数据修改数据库状态成功，数据同步失败; ';
    		}else{
    			$info = '';
    		}
    		
    	}else{
    		$info= '数据修改数据库状态错误且未进行同步; ';
    	}
    	
    	return $info;
    }
    
    
    /**
     * 版本更新（停服）
     * @param unknown $ids
     * @return multitype:number string
     */
    public static function version_stop_server($ids){
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    	
    	$l_info = '';
    	$show_flag = true;
    	foreach ($list as $key=>$val){
    		if($val['version_update'] != 0){
    			$version_update = intval($val['version_update'])+1;
    			$l_info .= "序号：".$val['id']." 的数据应该从第".$version_update."步骤开始; "."\n<br/>";
    			$show_flag = false;
    		}
    	}
    	
    	if(!$show_flag){
    		$out = array('status'=>0,'msg'=>$l_info,'data'=>'');
    		return $out;
    	}
    	
    	
    	$rInfo = '';
    	foreach ($list as $key=>$val){
    		$exec_l = 'sudo /data/sh/web/wupdate_game_version.sh stop '.$val['develop_id'].' '.$val['ip'];
    		//$exec_l = 'dir';
    		exec($exec_l,$output,$return_var);
    		$jb_out = '';
    		foreach ($output as $ok=>$ov){
    			$jb_out .= $ov.",\n<br/>";
    		}
    		
    		if($return_var == 0){  //成功,则需改变状态且改变游戏边的状态
    			$opsInfo = self::opsUpdate($val,2,2,1);
    			if($opsInfo == ''){ //修改状态和同步成功
    				$rInfo .= "序号：".$val['id']." 的数据停服成功; "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}else{
    				$rInfo .= "序号：".$val['id']." 的数据执行停服脚本成功， ".$opsInfo."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}
    		}else{
    			$rInfo .= "序号：".$val['id']." 的数据执行停服脚本失败; "."\n<br/>";
    			$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    		}
    	}
    	
    	$out = array('status'=>1,'msg'=>$rInfo,'data'=>'');
    	return $out;
    }
    
    /**
     * 版本更新（分发更新包）
     * @param unknown $ids
     * @return multitype:number string
     */
    public static function version_hand_out($ids){
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    	 
    	$l_info = '';
    	$show_flag = true;
    	foreach ($list as $key=>$val){
    		if($val['version_update'] != 1){
    			$version_update = intval($val['version_update'])+1;
    			$l_info .= "序号：".$val['id']." 的数据应该从第".$version_update."步骤开始; "."\n<br/>";
    			$show_flag = false;
    		}
    	}
    	 
    	if(!$show_flag){
    		$out = array('status'=>0,'msg'=>$l_info,'data'=>'');
    		return $out;
    	}
    	
    	$rInfo = '';
    	foreach ($list as $key=>$val){
    		$exec_l = 'sudo /data/sh/web/wupdate_game_version.sh scp_server '.$val['develop_id'].' '.$val['ip'];
    		//$exec_l = 'dir';
    		exec($exec_l,$output,$return_var);
    		$jb_out = '';
    		foreach ($output as $ok=>$ov){
    			$jb_out .= $ov.",\n<br/>";
    		}
    	
    		if($return_var == 0){  //成功,则需改变状态且改变游戏边的状态
    			$update = array();
    			$update['version_update']   = 2;
    			$sq_result = $modServer->updateByPk($val['id'], $update);
    			
    			if($sq_result){ //修改成功
    				$rInfo .= "序号：".$val['id']." 的数据分发更新包成功; "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}else{
    				$rInfo .= "序号：".$val['id']." 的数据执行分发更新包脚本成功，数据操作失败 "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}
    		}else{
    			$rInfo .= "序号：".$val['id']." 的数据执行分发更新包脚本失败; "."\n<br/>";
    			$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    		}
    	}
    	 
    	$out = array('status'=>1,'msg'=>$rInfo,'data'=>'');
    	return $out;
    	
    }
    
    
    /**
     * 版本更新（检查更新包）
     * @param unknown $ids
     * @return multitype:number string
     */
    public static function version_check($ids){
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    
    	$l_info = '';
    	$show_flag = true;
    	foreach ($list as $key=>$val){
    		if($val['version_update'] != 2){
    			$version_update = intval($val['version_update'])+1;
    			$l_info .= "序号：".$val['id']." 的数据应该从第".$version_update."步骤开始; "."\n<br/>";
    			$show_flag = false;
    		}
    	}
    
    	if(!$show_flag){
    		$out = array('status'=>0,'msg'=>$l_info,'data'=>'');
    		return $out;
    	}
    	 
    	$rInfo = '';
    	foreach ($list as $key=>$val){
    		$exec_l = 'sudo /data/sh/web/wupdate_game_version.sh check_server '.$val['develop_id'].' '.$val['ip'];
    		//$exec_l = 'dir';
    		exec($exec_l,$output,$return_var);
    		$jb_out = '';
    		foreach ($output as $ok=>$ov){
    			$jb_out .= $ov.",\n<br/>";
    		}
    		 
    		if($return_var == 0){  //成功,则需改变状态且改变游戏边的状态
    			$update = array();
    			$update['version_update']   = 3;
    			$sq_result = $modServer->updateByPk($val['id'], $update);
    			 
    			if($sq_result){ //修改成功
    				$rInfo .= "序号：".$val['id']." 的数据检查更新包成功; "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}else{
    				$rInfo .= "序号：".$val['id']." 的数据执行检查更新包脚本成功，数据操作失败 "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}
    		}else{
    			$rInfo .= "序号：".$val['id']." 的数据执行检查更新包脚本失败; "."\n<br/>";
    			$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    		}
    	}
    
    	$out = array('status'=>1,'msg'=>$rInfo,'data'=>'');
    	return $out;
    	 
    }
    
    /**
     * 版本更新（更新）
     * @param unknown $ids
     * @return multitype:number string
     */
    public static function version_update_oper($ids){
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    
    	$l_info = '';
    	$show_flag = true;
    	foreach ($list as $key=>$val){
    		if($val['version_update'] != 3){
    			$version_update = intval($val['version_update'])+1;
    			$l_info .= "序号：".$val['id']." 的数据应该从第".$version_update."步骤开始; "."\n<br/>";
    			$show_flag = false;
    		}
    	}
    
    	if(!$show_flag){
    		$out = array('status'=>0,'msg'=>$l_info,'data'=>'');
    		return $out;
    	}
    
    	$rInfo = '';
    	foreach ($list as $key=>$val){
    		$exec_l = 'sudo /data/sh/web/wupdate_game_version.sh update_server '.$val['develop_id'].' '.$val['ip'];
    		//$exec_l = 'dir';
    		exec($exec_l,$output,$return_var);
    		$jb_out = '';
    		foreach ($output as $ok=>$ov){
    			$jb_out .= $ov.",\n<br/>";
    		}
    		 
    		if($return_var == 0){  //成功,则需改变状态且改变游戏边的状态
    			$update = array();
    			$update['version_update'] = 4;
    			$sq_result = $modServer->updateByPk($val['id'], $update);
    
    			if($sq_result){ //修改成功
    				$rInfo .= "序号：".$val['id']." 的数据更新包成功; "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}else{
    				$rInfo .= "序号：".$val['id']." 的数据执行更新包脚本成功，数据操作失败 "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}
    		}else{
    			$rInfo .= "序号：".$val['id']." 的数据执行更新包脚本失败; "."\n<br/>";
    			$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    		}
    	}
    
    	$out = array('status'=>1,'msg'=>$rInfo,'data'=>'');
    	return $out;
    
    }
    
    /**
     * 版本更新（检查版本号）
     * @param unknown $ids
     * @return multitype:number string
     */
    public static function version_test($ids){
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    
    	$l_info = '';
    	$show_flag = true;
    	foreach ($list as $key=>$val){
    		if($val['version_update'] != 4){
    			$version_update = intval($val['version_update'])+1;
    			$l_info .= "序号：".$val['id']." 的数据应该从第".$version_update."步骤开始; "."\n<br/>";
    			$show_flag = false;
    		}
    	}
    
    	if(!$show_flag){
    		$out = array('status'=>0,'msg'=>$l_info,'data'=>'');
    		return $out;
    	}
    
    	$rInfo = '';
    	foreach ($list as $key=>$val){
    		$exec_l = 'sudo /data/sh/web/wupdate_game_version.sh check_ver '.$val['develop_id'].' '.$val['ip'];
    		//$exec_l = 'dir';
    		exec($exec_l,$output,$return_var);
    		$jb_out = '';
    		foreach ($output as $ok=>$ov){
    			$jb_out .= $ov.",\n<br/>";
    		}
    
    		if($return_var == 0){  //成功,则需改变状态且改变游戏边的状态
    			$update = array();
    			$update['version_update'] = 5;
    			$sq_result = $modServer->updateByPk($val['id'], $update);
    			 
    			if($sq_result){ //修改成功
    				$rInfo .= "序号：".$val['id']." 的数据检查版本号成功; "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}else{
    				$rInfo .= "序号：".$val['id']." 的数据检查版本号脚本成功，数据操作失败 "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}
    		}else{
    			$rInfo .= "序号：".$val['id']." 的数据执行测试脚本失败; "."\n<br/>";
    			$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    		}
    	}
    
    	$out = array('status'=>1,'msg'=>$rInfo,'data'=>'');
    	return $out;
    }
    
    
    /**
     * 版本更新（开服）
     * @param unknown $ids
     * @return multitype:number string
     */
    public static function version_open_server($ids){
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    	 
    	$l_info = '';
    	$show_flag = true;
    	foreach ($list as $key=>$val){
    		if($val['version_update'] != 5){
    			$version_update = intval($val['version_update'])+1;
    			$l_info .= "序号：".$val['id']." 的数据应该从第".$version_update."步骤开始; "."\n<br/>";
    			$show_flag = false;
    		}
    	}
    	 
    	if(!$show_flag){
    		$out = array('status'=>0,'msg'=>$l_info,'data'=>'');
    		return $out;
    	}
    	 
    	 
    	$rInfo = '';
    	foreach ($list as $key=>$val){
    		$exec_l = 'sudo /data/sh/web/wupdate_game_version.sh start '.$val['develop_id'].' '.$val['ip'];
    		//$exec_l = 'dir';
    		exec($exec_l,$output,$return_var);
    		$jb_out = '';
    		foreach ($output as $ok=>$ov){
    			$jb_out .= $ov.",\n<br/>";
    		}
    
    		if($return_var == 0){  //成功,则需改变状态且改变游戏边的状态
    			$opsInfo = self::opsUpdate($val,2,1,0);
    			if($opsInfo == ''){ //修改状态和同步成功
    				$rInfo .= "序号：".$val['id']." 的数据开服成功; "."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}else{
    				$rInfo .= "序号：".$val['id']." 的数据执行开服脚本成功， ".$opsInfo."\n<br/>";
    				$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    			}
    		}else{
    			$rInfo .= "序号：".$val['id']." 的数据执行开服脚本失败; "."\n<br/>";
    			$rInfo .= '脚本输出内容：'.$jb_out."\n<br/>";
    		}
    	}
    	 
    	$out = array('status'=>1,'msg'=>$rInfo,'data'=>'');
    	return $out;
    }
    

    /**
     * 版本更新（操作回到上一步）
     * @param unknown $ids
     * @return multitype:number string
     */
    public static function version_return($ids,$version_update){
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    	foreach ($list as $key=>$val){
    		$update = array();
    		$update['version_update'] = $version_update;
    		$sq_result = $modServer->updateByPk($val['id'], $update);
    	}
    	$out = array('status'=>1,'msg'=>'操作成功','data'=>'');
    	return $out;
    }
    
    /**
     * 批量修改状态
     */
    public static function updateStatus($ids,$status){
    	$showStatusList = self::showStatusList();
    	//根据ids查询出服务器列表信息
    	$game = PubBase::getCurrentGame();
    	$modServer = new ModServer();
    	$list = $modServer->getServerSpeForIds($game,$ids);
    	$tmp = time();
    	$show_flag = true;
    	$info = '序号为 ';
    	foreach ($list as $key=>$val){
    		if($val['show_status'] == $status){
    			$show_flag = false;
    			$info .= $val['id'] . ', ';
    		}
    	}
    	 
    	$info = substr($info,0,strlen($info)-2);
    	$info .= ' 的数据不符合把表现状态切换为'.$showStatusList[$status].'的条件';
    	if(!$show_flag){
    		$out = array('status'=>0,'msg'=>$info,'data'=>'');
    		return $out;
    	}

    	$rInfo = '';
    	foreach ($list as $key=>$val){
    		$opsInfo = self::batchUpdate($val,$status);
    		if($opsInfo == ''){ //修改为正常状态和同步成功
    			$rInfo .= "序号：".$val['id']." 的数据切换状态成功; "."\n<br/>";
    		}else{
    			$rInfo .= "序号：".$val['id']." ".$opsInfo."\n<br/>";
    		}
    	}
    	 
    	$out = array('status'=>1,'msg'=>$rInfo,'data'=>'');
    	return $out;
    }
    
    public static function batchUpdate($val,$show_status){
    	$modServer = new ModServer();
    	$update = array();
    	$update['show_status']   = $show_status;
    	$sq_result = $modServer->updateByPk($val['id'], $update);
    	if($sq_result !== false){
    		$synchro = array();
    		$synchro['name'] = $val['name'];
    		$synchro['show_status'] = intval($show_status);
    		$synchro['ip'] = $val['ip'];
    		$synchro['port'] = intval($val['port']);
    		$synchro['float_status'] = intval($val['float_status']);
    		$synchro['open_time'] = $val['open_time'];
    		$synchro['show_name'] = $val['server_name'];
    		$synchro['server_id'] = $val['server_num'];
    		$api = new ApiSynchro();
    		$s_result = $api->gameEdit($synchro,$val['api_synchro_url']);
    		//$s_result['state'] = true;
    		if(!$s_result['state']){ //同步失败
    			$update = array();
    			$update['is_syn'] = 3;
    			$update['syn_time'] = time();
    			$modServer->updateByPk($val['id'], $update);
    			$info= '数据修改数据库状态成功，数据同步失败; ';
    		}else{
    			$info = '';
    		}
    
    	}else{
    		$info= '数据修改数据库状态错误且未进行同步; ';
    	}
    	 
    	return $info;
    }
    
    
    public static function fixedTimeHeader(){
    	$header = array(
    			'id'            => array('text' => '序号', 'width' => '90px'),
    			'server_name'   => array('text' => '服务器名称', 'width' => '160px'),
    			'server_num'    => array('text' => '服务器ID', 'width' => '100px'),    		
    			'show_status'   => array('text' => '表现状态', 'width' => '160px'),
    			'special_name'   => array('text' => '所属专服', 'width' => '160px'),
    			'channel_list'   => array('text' => '渠道列表', 'width' => '200px'),
    			'open_time'     => array('text' => '开服时间', 'width' => '140px'),
    			'clear_time'     => array('text' => '清档时间', 'width' => '140px'),
    			'info'          => array('text' => '任务情况', 'width' => '300px'),
    			'action'        => array('text' => '操作', 'width' => '60px'),
    	);
    	return $header;
    }
    
    public static function fixedTimeData($params){
    	$specialList = array();
    	$modServer = new ModServer();
    	$result = $modServer->page($params);
    
    	$modSpecialServicer = new ModSpecialServicer();
    	$spList = $modSpecialServicer->getSpecialServer();
    	$specialList = array();
    	foreach ($spList as $key=>$val){
    		$specialList[$val['server_id']]['name'] = $val['name'];
    		$specialList[$val['server_id']]['cname'] = $val['cname'];
    		$specialList[$val['server_id']]['special_flag_id'] = $val['special_flag_id'];
    	}
    
    	$length = count($result['data']);
    	$showStatusList = self::showStatusList();
    	foreach($result['data'] as &$v){
    		$type = 1;
    		if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
    			$type = 2;
    		}
    		$v['clear_time']     = $v['clear_time']? date('Y-m-d H:i:s', $v['clear_time']) : '';
    		$v['channel_list'] = self::getChannel($v['id']);
    		$v['special_name'] = $specialList[$v['id']]['name'];
    		$show_status = $v['show_status'];
    		$v['show_status'] = $showStatusList[$v['show_status']];
    		
    		if($v['open_time'] > time()){ //开服时间大于当前时间的才能修改定时的清档时间
    			$v['action']        = ' <a href="'.PubFunc::buildBackUrl('/oss/?r=server/fixedTimeEdit').'&id='.$v['id'].'">编辑</a>';
    		}
    		$v['open_time']     = $v['open_time']? date('Y-m-d H:i:s', $v['open_time']) : '';
    		
    		if($v['is_automatic'] == 2){ //已经跑过
    			$v['info'] = '<div style="white-space:normal;">'.$v['automatic_info'].'</div>';
    		}
    	}
    	unset($v);
    	return $result;
    }

    public static function fixedTimeDoEdit($id, $params){
    	$return_result = array();
    	if(empty($id)){
    		$return_result = array('state'=>false,'msg'=>'非法参数');
    		return $return_result;
    	}
    	
    	if(strtotime($params['clear_time']) < time()){
    		$return_result = array('state'=>false,'msg'=>'设置的时间要大于当前时间');
    		//return $return_result;
    	}

    	$modServer = new ModServer();
    	$update = array();
    	$update['clear_time'] = strtotime($params['clear_time']);
    	$update['is_automatic'] = 1;
    	$result = $modServer->updateByPk($id, $update);
    	$return_result = array('state'=>true,'msg'=>'修改成功');
    	return $return_result;
    }
    
    
    public static function versionFixedTimeHeader(){
    	$header = array(
				'id'            => array('text' => '序号', 'width' => '90px'),
				'server_name'   => array('text' => '服务器名称', 'width' => '160px'),
    			'special_name'   => array('text' => '所属专服', 'width' => '160px'),
				'show_status'   => array('text' => '表现状态', 'width' => '100px'),
				'develop_id'    => array('text' => '开发ID', 'width' => '160px'),
				'oss_api'       => array('text' => 'OSS API', 'width' => '200px'),
				'ip_port'       => array('text' => 'IP:端口', 'width' => '120px'),
    			'version_update_time'     => array('text' => '版本更新时间', 'width' => '140px'),
    			'update_packet'     => array('text' => '版本更新包', 'width' => '140px'),
    			'info'          => array('text' => '任务情况', 'width' => '300px'),
    			'action'        => array('text' => '操作', 'width' => '60px'),
		);
		return $header;
    }
    
    public static function versionFixedTimeData($params){    	
    	$modServer = new ModServer();
    	$result = $modServer->page($params);

    	$serverIds = '';
    	foreach ($result['data'] as $key=>$val){
    		$serverIds .= ','.$val['id'];
    	}
    	$serverIds = substr($serverIds, 1);
    	$modVersionUpdate = new ModVersionUpdate();
    	$versionUpdateList = $modVersionUpdate->getVersionUpdate($serverIds);
    	$versionList = array();
    	foreach ($versionUpdateList as $key=>$val){
    		$versionList[$val['server_id']] = $val;
    	}

    	$length = count($result['data']);
    	$showStatusList = self::showStatusList();
    	
    	$modSpecialServicer = new ModSpecialServicer();
    	$spList = $modSpecialServicer->getSpecialServer();
    	$specialList = array();
    	foreach ($spList as $key=>$val){
    		$specialList[$val['server_id']]['name'] = $val['name'];
    		$specialList[$val['server_id']]['cname'] = $val['cname'];
    		$specialList[$val['server_id']]['special_flag_id'] = $val['special_flag_id'];
    	}
    	
    	foreach($result['data'] as &$v){
    		$type = 1;
    		if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
    			$type = 2;
    		}
    		$v['special_name'] = $specialList[$v['id']]['name'];
    		$v['version_update_time'] = $versionList[$v['id']]['version_update_time']? date('Y-m-d H:i:s', $versionList[$v['id']]['version_update_time']) : '';
    		if($versionList[$v['id']]['version_is_automatic'] == 2){ //已经跑过
    			$v['info'] = '<div style="white-space:normal;">'.$versionList[$v['id']]['version_automatic_info'].'</div>';
    		}
    		$v['update_packet'] = $versionList[$v['id']]['update_packet'];
    		
    		$v['action']        .= '&nbsp;&nbsp;<a href="javascript:;" name="version_update_one" data-id="'.$v['id'].'">设置</a>';
	
    		$id = $v['id'];
    		$checkbox = '<input value="'.$v['id'].'" data-status="'.$v['show_status'].'" data-tmp="'.$v['open_time'].'" data-wlk="'.$v['is_wlk'].'" name="id" type="checkbox">&nbsp;&nbsp;';
    		$v['id'] = $checkbox.$v['id'];	
    		$v['show_status'] = $showStatusList[$v['show_status']];
    		$f = $v['port']? ':' : '';
    		$v['ip_port'] = $v['ip'].$f.$v['port'];
    		
    		
    	}
    	unset($v);
    	
    	return $result;
    }
    
    /**
     * 修改定时版本更新时间
     * @param unknown $ids
     * @param unknown $version_update_time
     */
    public static function setVersionTime($ids,$version_update_time,$update_packet){
    	if($ids == ''){
    		$out = array('status'=>0,'msg'=>'请选择数据','data'=>'');
    		return $out;
    	}
    	
    	if($version_update_time == ''){
    		$out = array('status'=>0,'msg'=>'请设置版本更新时间','data'=>'');
    		return $out;
    	}
    	
    	if($update_packet == ''){
    		$out = array('status'=>0,'msg'=>'请设置更新包名','data'=>'');
    		return $out;
    	}
    	//先删除后添加
    	$modVersionUpdate = new ModVersionUpdate();
    	$modVersionUpdate->deleteForServerId($ids);
    	
    	$ids_array = explode(',', $ids);
    	$ids_array = array_unique($ids_array);
    	$data = array();
    	foreach ($ids_array as $key=>$val){
    		$d = array();
    		$d['server_id'] = $val;
    		$d['version_is_automatic'] = 1;
    		$d['version_update_time'] = strtotime($version_update_time);
    		$d['update_packet'] = $update_packet;
    		$d['create_time'] = time();
    		array_push($data, $d);
    	}
    	
    	$modVersionUpdate->addmuch($data);
    	$out = array('status'=>1,'msg'=>'操作成功','data'=>'');
    	return $out;
    }
    
}